/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.yjf.ljr.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import android.app.Activity;
import android.app.Service;
import android.app.admin.DevicePolicyManager;
import android.content.ContentResolver;
import android.content.Context;
import android.os.SystemClock;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;

import com.yjf.ljr.activity.LockPatternSettingActivity;
import com.yjf.ljr.application.MyApplication;
import com.yjf.ljr.common.CommonData;
import com.yjf.ljr.view.LockPatternView;


/**
 * Utilities for the lock patten and its settings.
 */
public class LockPatternUtils {

	private static final String TAG = "LockPatternUtils";

	private static final String LOCK_PATTERN_FILE = "gesture.key"/*"/gesture.key"*/;

	/**
	 * The maximum number of incorrect attempts before the user is prevented
	 * from trying again for {@link #FAILED_ATTEMPT_TIMEOUT_MS}.
	 */
	public static final int FAILED_ATTEMPTS_BEFORE_TIMEOUT = 5;

	/**
	 * The number of incorrect attempts before which we fall back on an
	 * alternative method of verifying the user, and resetting their lock
	 * pattern.
	 */
	public static final int FAILED_ATTEMPTS_BEFORE_RESET = 20;

	/**
	 * How long the user is prevented from trying again after entering the wrong
	 * pattern too many times.
	 */
	public static final long FAILED_ATTEMPT_TIMEOUT_MS = 30000L;

	/**
	 * The interval of the countdown for showing progress of the lockout.
	 */
	public static final long FAILED_ATTEMPT_COUNTDOWN_INTERVAL_MS = 1000L;

	/**
	 * The minimum number of dots in a valid pattern.
	 */
	public static final int MIN_LOCK_PATTERN_SIZE = 4;

	/**
	 * The minimum number of dots the user must include in a wrong pattern
	 * attempt for it to be counted against the counts that affect
	 * {@link #FAILED_ATTEMPTS_BEFORE_TIMEOUT} and
	 * {@link #FAILED_ATTEMPTS_BEFORE_RESET}
	 */
	public static final int MIN_PATTERN_REGISTER_FAIL = 3;

	private final static String LOCKOUT_PERMANENT_KEY = "lockscreen.lockedoutpermanently";
	private final static String LOCKOUT_ATTEMPT_DEADLINE = "lockscreen.lockoutattemptdeadline";
	private final static String PATTERN_EVER_CHOSEN_KEY = "lockscreen.patterneverchosen";
	public final static String PASSWORD_TYPE_KEY = "lockscreen.password_type";
	
	private final static String LOCK_PATTERN_ENABLED = "lock_pattern_autolock";
	private final static String LOCK_PATTERN_VISIBLE = "lock_pattern_visible_pattern";
	private final static String LOCK_PATTERN_TACTILE_FEEDBACK_ENABLED = "lock_pattern_tactile_feedback_enabled";

	private final Context mContext;
	private final ContentResolver mContentResolver;
	private static String sLockPatternFilename;
	private static String originLockPatternFilename;

	/**
	 * @param contentResolver
	 *            Used to look up and save settings.
	 */
	public LockPatternUtils(Context context) {
		mContext = context;
		mContentResolver = context.getContentResolver();
		// Initialize the location of gesture lock file
		if(originLockPatternFilename == null) {
			originLockPatternFilename = mContext.getFilesDir().getAbsolutePath() +"/";
		}
		if (sLockPatternFilename == null) {
			sLockPatternFilename = originLockPatternFilename/*mContext.getFilesDir().getAbsolutePath() +"/"*//*+ LOCK_PATTERN_FILE*/;			
		}
	}

	/**
	 * Check to see if a pattern matches the saved pattern. If no pattern
	 * exists, always returns true.
	 * 
	 * @param pattern
	 *            The pattern to check.
	 * @return Whether the pattern matches the stored one.
	 */
	public boolean checkPattern(List<LockPatternView.Cell> pattern) {
		try {
			// Read all the bytes from the file
			//获取对应用户的手势密码
			sLockPatternFilename = originLockPatternFilename + CommonData.USER_NAME+LOCK_PATTERN_FILE;
			RandomAccessFile raf = new RandomAccessFile(sLockPatternFilename,
					"r");
			final byte[] stored = new byte[(int) raf.length()];
			int got = raf.read(stored, 0, stored.length);
			raf.close();
			if (got <= 0) {
				return true;
			}
			// Compare the hash from the file with the entered pattern's hash
			return Arrays.equals(stored,
					LockPatternUtils.patternToHash(pattern));
		} catch (FileNotFoundException fnfe) {
			return true;
		} catch (IOException ioe) {
			return true;
		}
	}	

	/**
	 * Checks to see if the given file exists and contains any data. Returns
	 * true if it does, false otherwise.
	 * 
	 * @param filename
	 * @return true if file exists and is non-empty.
	 */
	private boolean nonEmptyFileExists(String filename) {
		try {
			// Check if we can read a byte from the file
			RandomAccessFile raf = new RandomAccessFile(filename, "r");
			raf.readByte();
			raf.close();
			return true;
		} catch (FileNotFoundException fnfe) {
			return false;
		} catch (IOException ioe) {
			return false;
		}
	}

	/**
	 * Check to see if the user has stored a lock pattern.
	 * 
	 * @return Whether a saved pattern exists.
	 */
	public boolean savedPatternExists() {
		sLockPatternFilename = originLockPatternFilename + CommonData.USER_NAME+LOCK_PATTERN_FILE;
		return nonEmptyFileExists(sLockPatternFilename);
	}

	/**
	 * Return true if the user has ever chosen a pattern. This is true even if
	 * the pattern is currently cleared.
	 * 
	 * @return True if the user has ever chosen a pattern.
	 */
	public boolean isPatternEverChosen() {
		return getBoolean(CommonData.USER_NAME+PATTERN_EVER_CHOSEN_KEY);
	}

	/**
	 * Clear any lock pattern or password.
	 */
	public void clearLock() {
		setLockPatternEnabled(false);
		saveLockPattern(null);
		setLong(CommonData.USER_NAME + PASSWORD_TYPE_KEY, DevicePolicyManager.PASSWORD_QUALITY_SOMETHING);
		setBoolean(CommonData.USER_NAME + PATTERN_EVER_CHOSEN_KEY, false);
	}

	/**
	 * Save a lock pattern.
	 * 
	 * @param pattern
	 *            The new pattern to save.
	 */
	public void saveLockPattern(List<LockPatternView.Cell> pattern) {
		// Compute the hash
		final byte[] hash = LockPatternUtils.patternToHash(pattern);
		try {
			// Write the hash to file
			//存储对应用户的手势密码
			sLockPatternFilename = originLockPatternFilename + CommonData.USER_NAME+LOCK_PATTERN_FILE;
			RandomAccessFile raf = new RandomAccessFile(sLockPatternFilename,
					"rw");
			// Truncate the file if pattern is null, to clear the lock
			if (pattern == null) {
				raf.setLength(0);
			} else {
				raf.write(hash, 0, hash.length);
			}
			raf.close();
			if (pattern != null) {
				setBoolean(CommonData.USER_NAME+PATTERN_EVER_CHOSEN_KEY, true);
				setLong(CommonData.USER_NAME+PASSWORD_TYPE_KEY,
						DevicePolicyManager.PASSWORD_QUALITY_SOMETHING);				
			}
		} catch (FileNotFoundException fnfe) {
			// Cant do much, unless we want to fail over to using the settings
			// provider
			Log.e(TAG, "Unable to save lock pattern to " + sLockPatternFilename);
		} catch (IOException ioe) {
			// Cant do much
			Log.e(TAG, "Unable to save lock pattern to " + sLockPatternFilename);
		}
	}

	public void deleteLockPattern() {
		setLockPatternEnabled(false);
		setLong(CommonData.USER_NAME + PASSWORD_TYPE_KEY, DevicePolicyManager.PASSWORD_QUALITY_SOMETHING);
		setBoolean(CommonData.USER_NAME + PATTERN_EVER_CHOSEN_KEY, false);
		SPreferences.saveData(mContext, false, CommonData.USER_NAME+LockPatternSettingActivity.LOCK_PATTERN_IS_OPEN);
        //关闭自动登陆	
		MyApplication myApplication = null;
		if(mContext instanceof Activity) {
			myApplication = (MyApplication)((Activity)mContext).getApplication();
		} else if(mContext instanceof Service) {
			myApplication = (MyApplication)((Service)mContext).getApplication();
		}
		if(myApplication != null) {
			myApplication.stopAutoLogin();
		}
		sLockPatternFilename = originLockPatternFilename + CommonData.USER_NAME+LOCK_PATTERN_FILE;
		File file = new File(sLockPatternFilename);
		if(file.exists() && file.isFile()) {
			file.delete();
		}
	}
	
	/**
	 * Compute the password quality from the given password string.
	 */
	static public int computePasswordQuality(String password) {
		boolean hasDigit = false;
		boolean hasNonDigit = false;
		final int len = password.length();
		for (int i = 0; i < len; i++) {
			if (Character.isDigit(password.charAt(i))) {
				hasDigit = true;
			} else {
				hasNonDigit = true;
			}
		}
		if (hasNonDigit && hasDigit) {
			return DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC;
		}
		if (hasNonDigit) {
			return DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC;
		}
		if (hasDigit) {
			return DevicePolicyManager.PASSWORD_QUALITY_NUMERIC;
		}
		return DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED;
	}

	/**
	 * Deserialize a pattern.
	 * 
	 * @param string
	 *            The pattern serialized with {@link #patternToString}
	 * @return The pattern.
	 */
	public static List<LockPatternView.Cell> stringToPattern(String string) {
		List<LockPatternView.Cell> result = new ArrayList<LockPatternView.Cell>();
		final byte[] bytes = string.getBytes();
		for (int i = 0; i < bytes.length; i++) {
			byte b = bytes[i];
			result.add(LockPatternView.Cell.of(b / 3, b % 3));
		}
		return result;
	}

	/**
	 * Serialize a pattern.
	 * 
	 * @param pattern
	 *            The pattern.
	 * @return The pattern in string form.
	 */
	public static String patternToString(List<LockPatternView.Cell> pattern) {
		if (pattern == null) {
			return "";
		}
		final int patternSize = pattern.size();
		byte[] res = new byte[patternSize];
		for (int i = 0; i < patternSize; i++) {
			LockPatternView.Cell cell = pattern.get(i);
			res[i] = (byte) (cell.getRow() * 3 + cell.getColumn());
		}
		return new String(res);
	}

	/*
	 * Generate an SHA-1 hash for the pattern. Not the most secure, but it is at
	 * least a second level of protection. First level is that the file is in a
	 * location only readable by the system process.
	 * 
	 * @param pattern the gesture pattern.
	 * 
	 * @return the hash of the pattern in a byte array.
	 */
	private static byte[] patternToHash(List<LockPatternView.Cell> pattern) {
		if (pattern == null) {
			return null;
		}

		final int patternSize = pattern.size();
		byte[] res = new byte[patternSize];
		for (int i = 0; i < patternSize; i++) {
			LockPatternView.Cell cell = pattern.get(i);
			res[i] = (byte) (cell.getRow() * 3 + cell.getColumn());
		}
		try {
			MessageDigest md = MessageDigest.getInstance("SHA-1");
			byte[] hash = md.digest(res);
			Log.d("test", "hash is "+hash);
			return hash;
		} catch (NoSuchAlgorithmException nsa) {
			return res;
		}
	}
	
	/**
	 * @return Whether the lock pattern is enabled.
	 */
	public boolean isLockPatternEnabled() {
		return getBoolean(CommonData.USER_NAME+LOCK_PATTERN_ENABLED)
				&& getLong(CommonData.USER_NAME+PASSWORD_TYPE_KEY,
						DevicePolicyManager.PASSWORD_QUALITY_SOMETHING) == DevicePolicyManager.PASSWORD_QUALITY_SOMETHING;
	}

	/**
	 * Set whether the lock pattern is enabled.
	 */
	public void setLockPatternEnabled(boolean enabled) {
		setBoolean(CommonData.USER_NAME+LOCK_PATTERN_ENABLED, enabled);
	}

	/**
	 * @return Whether the visible pattern is enabled.
	 */
	public boolean isVisiblePatternEnabled() {
		return getBoolean(LOCK_PATTERN_VISIBLE);
	}

	/**
	 * Set whether the visible pattern is enabled.
	 */
	public void setVisiblePatternEnabled(boolean enabled) {
		setBoolean(LOCK_PATTERN_VISIBLE, enabled);
	}

	/**
	 * @return Whether tactile feedback for the pattern is enabled.
	 */
	public boolean isTactileFeedbackEnabled() {
		return getBoolean(CommonData.USER_NAME+LOCK_PATTERN_TACTILE_FEEDBACK_ENABLED);
	}

	/**
	 * Set whether tactile feedback for the pattern is enabled.
	 */
	public void setTactileFeedbackEnabled(boolean enabled) {
		setBoolean(CommonData.USER_NAME+LOCK_PATTERN_TACTILE_FEEDBACK_ENABLED,
				enabled);
	}

	/**
	 * Set and store the lockout deadline, meaning the user can't attempt
	 * his/her unlock pattern until the deadline has passed.
	 * 
	 * @return the chosen deadline.
	 */
	public long setLockoutAttemptDeadline() {
		final long deadline = SystemClock.elapsedRealtime()
				+ FAILED_ATTEMPT_TIMEOUT_MS;
		setLong(LOCKOUT_ATTEMPT_DEADLINE, deadline);
		return deadline;
	}

	/**
	 * @return The elapsed time in millis in the future when the user is allowed
	 *         to attempt to enter his/her lock pattern, or 0 if the user is
	 *         welcome to enter a pattern.
	 */
	public long getLockoutAttemptDeadline() {
		final long deadline = getLong(LOCKOUT_ATTEMPT_DEADLINE, 0L);
		final long now = SystemClock.elapsedRealtime();
		if (deadline < now || deadline > (now + FAILED_ATTEMPT_TIMEOUT_MS)) {
			return 0L;
		}
		return deadline;
	}

	/**
	 * @return Whether the user is permanently locked out until they verify
	 *         their credentials. Occurs after
	 *         {@link #FAILED_ATTEMPTS_BEFORE_RESET} failed attempts.
	 */
	public boolean isPermanentlyLocked() {
		return getBoolean(LOCKOUT_PERMANENT_KEY);
	}

	/**
	 * Set the state of whether the device is permanently locked, meaning the
	 * user must authenticate via other means.
	 * 
	 * @param locked
	 *            Whether the user is permanently locked out until they verify
	 *            their credentials. Occurs after
	 *            {@link #FAILED_ATTEMPTS_BEFORE_RESET} failed attempts.
	 */
	public void setPermanentlyLocked(boolean locked) {
		setBoolean(LOCKOUT_PERMANENT_KEY, locked);
	}

	/**
	 * @return A formatted string of the next alarm (for showing on the lock
	 *         screen), or null if there is no next alarm.
	 */
	public String getNextAlarm() {
		String nextAlarm = Settings.System.getString(mContentResolver,
				Settings.System.NEXT_ALARM_FORMATTED);
		if (nextAlarm == null || TextUtils.isEmpty(nextAlarm)) {
			return null;
		}
		return nextAlarm;
	}

	private boolean getBoolean(String secureSettingKey) {
		return (Boolean)SPreferences.getData(mContext, false, secureSettingKey);
	}

	private void setBoolean(String secureSettingKey, boolean enabled) {
		SPreferences.saveData(mContext, enabled, secureSettingKey);
	}

	private long getLong(String secureSettingKey, long def) {
		return (Long)SPreferences.getData(mContext, def, secureSettingKey);
	}

	private void setLong(String secureSettingKey, long value) {
		SPreferences.saveData(mContext, value, secureSettingKey);
	}

	public boolean isSecure() {
		final boolean secure = isLockPatternEnabled() && savedPatternExists();
		return secure;
	}
}
